home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 25 / CU Amiga Magazine's Super CD-ROM 25 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-08].iso / CUCD / WWW / http / www.cu-amiga.co.uk / features / c-tutorial / Part-13.lzx / Part-13 / setf1.c < prev    next >
C/C++ Source or Header  |  2000-10-14  |  6KB  |  243 lines

  1. #include<exec/libraries.h>
  2. #include<exec/memory.h>
  3. #include<graphics/text.h>
  4. #include<intuition/intuition.h>
  5. #include<intuition/screens.h>
  6. #include<libraries/gadtools.h>
  7. #include<utility/tagitem.h>
  8.  
  9. #include<string.h>
  10. #include<stdio.h>
  11.  
  12. #include<clib/alib_protos.h>
  13. #include<clib/exec_protos.h>
  14. #include<clib/gadtools_protos.h>
  15. #include<clib/intuition_protos.h>
  16.  
  17. extern struct Library* SysBase;
  18.  
  19. /* The library base global variables */
  20. struct Library* IntuitionBase;
  21. struct Library* GadToolsBase;
  22.  
  23. /* Need to give prototypes for our functions */
  24. void handleIDCMP(struct Window*);
  25. void setupWindow();
  26. void createWindow(struct Gadget*);
  27.  
  28. #define MYFONTSIZE        (8)
  29.  
  30. /* Some constants for the size of the window */
  31. #define MYWIN_WIDTH        (410)
  32. #define MYWIN_HEIGHT    (210)
  33.  
  34. /* Some constants for the position and size of our gadget */
  35. #define MYGAD_LEFT        (10)
  36. #define MYGAD_TOP            (10+MYFONTSIZE)
  37. #define MYGAD_WIDTH        (MYWIN_WIDTH-MYGAD_LEFT*2)
  38. #define MYGAD_HEIGHT    (MYWIN_HEIGHT-MYGAD_TOP*2+MYFONTSIZE)
  39. #define MYGAD_TEXT        ("Result    Task                 Library          ")
  40. #define MYGAD_ID            (0)
  41.  
  42. /* Initialised structure declaration: describes standard Topaz font */
  43. static struct TextAttr topazFont = { "topaz.font", MYFONTSIZE, 0, 0, };
  44.  
  45. static struct Window* win;
  46. static struct Gadget* listgad;
  47. static struct List mylist;
  48. static struct SignalSemaphore ready;
  49.  
  50. /* The library offset of the OpenLibrary() call */
  51. /* (See <pragmas/exec_pragmas.h> for LVOs) */
  52. #define LVO_OPENLIBRARY (-0x228)
  53.  
  54. void addNode(char* name, struct Library* lib)
  55. {
  56.     struct Node* node = AllocVec(sizeof(struct Node), MEMF_PUBLIC | MEMF_CLEAR);
  57.     if(node && name)
  58.     {
  59.         struct Task* task = FindTask(NULL);
  60.         char* taskname = task->tc_Node.ln_Name;
  61.         int size = strlen(taskname);
  62.         if(size < 20)
  63.             size = 20;
  64.         size += 10+1+strlen(name)+1;
  65.         if(node->ln_Name = AllocVec(size, MEMF_PUBLIC))
  66.             sprintf(node->ln_Name, "$%08lx %-20s %s", lib, taskname, name);
  67.         GT_SetGadgetAttrs(listgad, win, NULL, GTLV_Labels, ~0, TAG_DONE);
  68.         AddHead(&mylist, node);
  69.         GT_SetGadgetAttrs(listgad, win, NULL, GTLV_Labels, &mylist, TAG_DONE);
  70.     }
  71. }
  72.  
  73. void freeNode(struct Node* node)
  74. {
  75.     FreeVec(node->ln_Name);
  76.     FreeVec(node);
  77. }
  78.  
  79. void freeList()
  80. {
  81.     struct Node* work;
  82.     struct Node* next = mylist.lh_Head;
  83.     while(next->ln_Succ)
  84.     {
  85.         work = next;
  86.         next = next->ln_Succ;
  87.         freeNode(work);
  88.     }
  89. }
  90.  
  91. typedef struct Library* (*FUNC)(register __a1 STRPTR name,
  92.                                                                 register __d0 ULONG vers);
  93.  
  94. FUNC oldf;
  95.  
  96. struct Library* __saveds newf(register __a1 STRPTR name,
  97.                                                             register __d0 ULONG vers)
  98. {
  99.     struct Library* result = oldf(name, vers);
  100.     /* Make sure the list/gadget is ready to be updated */
  101.     if(AttemptSemaphore(&ready))
  102.     {
  103.         addNode(name, result);
  104.         ReleaseSemaphore(&ready);
  105.     }
  106.     return result;
  107. }
  108.  
  109. /* The start of the program */
  110. void main()
  111. {
  112.     /* Open libraries... */
  113.     if(IntuitionBase = OpenLibrary("intuition.library",37))
  114.     {
  115.         if(GadToolsBase = OpenLibrary("gadtools.library",37))
  116.         {
  117.             /* Set up our semaphore and lock it */
  118.             InitSemaphore(&ready);
  119.             ObtainSemaphore(&ready);
  120.             oldf = SetFunction(SysBase, LVO_OPENLIBRARY, (APTR)&newf);
  121.             /* Now do the real work */
  122.             setupWindow();
  123.             SetFunction(SysBase, LVO_OPENLIBRARY, (APTR)oldf);
  124.             CloseLibrary(GadToolsBase);
  125.         }
  126.         else
  127.             printf("Error: could not open gadtools.library\n");
  128.         CloseLibrary(IntuitionBase);
  129.     }
  130.     else
  131.         printf("Error: could not open intuition.library\n");
  132. }
  133.  
  134. /* Setup the window -- do the GadTools stuff */
  135. void setupWindow()
  136. {
  137.     struct Screen* scr;
  138.     /* We'll copy the visual information for the default public screen */
  139.   /* (usually, this is the Workbench screen) */
  140.     if(scr = LockPubScreen(NULL))
  141.     {
  142.         APTR vinfo;
  143.         /* Get the visual info so GadTools can render the gadgets nicely */
  144.         if(vinfo = GetVisualInfo(scr, TAG_DONE))
  145.         {
  146.             struct Gadget* glist = NULL;
  147.             if(listgad = CreateContext(&glist))
  148.             {
  149.                 struct NewGadget newgad;
  150.                 /* The offsets of our window borders */
  151.                 int offleft = scr->WBorLeft;
  152.                 int offtop = scr->WBorTop + (scr->Font->ta_YSize + 1);
  153.                 /* Setup our first gadget */
  154.                 newgad.ng_TextAttr         = &topazFont;
  155.                 newgad.ng_VisualInfo     = vinfo;
  156.                 newgad.ng_LeftEdge         = MYGAD_LEFT + offleft;
  157.                 newgad.ng_TopEdge         = MYGAD_TOP + offtop;
  158.                 newgad.ng_Width             = MYGAD_WIDTH;
  159.                 newgad.ng_Height             = MYGAD_HEIGHT;
  160.                 newgad.ng_GadgetText    = MYGAD_TEXT;
  161.                 newgad.ng_GadgetID        = MYGAD_ID;
  162.                 newgad.ng_Flags                = 0;
  163.                 NewList(&mylist);
  164.                 /* Now create it and add it to our list */
  165.                 if(listgad = CreateGadget(LISTVIEW_KIND, listgad, &newgad,
  166.                                                                     GTLV_Labels, &mylist,
  167.                                                                     TAG_DONE))
  168.                     createWindow(glist);
  169.                 else
  170.                     printf("Error: could not create gadget(s)\n");
  171.                 /* Free the gadget */
  172.                 FreeGadgets(glist);
  173.                 freeList();
  174.             }
  175.             else
  176.                 printf("Error: could not create GadTools context\n");
  177.             FreeVisualInfo(vinfo);
  178.         }
  179.         else
  180.             printf("Error: could not get visual info\n");
  181.         UnlockPubScreen(NULL, scr);
  182.     }
  183.     else
  184.         printf("Error: could not lock public screen\n");
  185. }
  186.  
  187. /* Actually open the window, in the normal way */
  188. void createWindow(struct Gadget* glist)
  189. {
  190.     /* Open our window */
  191.     if(win = OpenWindowTags(NULL,
  192.                                                     WA_InnerWidth,    MYWIN_WIDTH,
  193.                                                     WA_InnerHeight,    MYWIN_HEIGHT,
  194.                                                     WA_Flags,        WFLG_CLOSEGADGET | WFLG_DRAGBAR,
  195.                                                     WA_IDCMP,        IDCMP_CLOSEWINDOW | IDCMP_REFRESHWINDOW,
  196.                                                     WA_Gadgets,    glist,
  197.                                                     TAG_DONE,        0))
  198.     {
  199.         /* Let GadTools refresh its bits of the window */
  200.         GT_RefreshWindow(win, NULL);
  201.         /* Now handle messages */
  202.         handleIDCMP(win);
  203.         CloseWindow(win);
  204.     }
  205.     else
  206.         printf("Error: could not open window\n");
  207. }
  208.  
  209. /* Our message handling code */
  210. void handleIDCMP(struct Window* win)
  211. {
  212.     int going = TRUE;
  213.     /* Let the patch work */
  214.     ReleaseSemaphore(&ready);
  215.     while(going)
  216.     {
  217.         struct IntuiMessage* intuimsg;
  218.         /* Wait for messages to arrive */
  219.         WaitPort(win->UserPort);
  220.         /* Messages have arrived: loop through all of them */
  221.         while(intuimsg = GT_GetIMsg(win->UserPort))
  222.         {
  223.             /* Act on this message... */
  224.             switch(intuimsg->Class)
  225.             {
  226.             case IDCMP_CLOSEWINDOW:
  227.                 going = FALSE;
  228.                 break;
  229.             case IDCMP_REFRESHWINDOW:
  230.                 /* You *MUST* remember to ask for and handle these refresh messages */
  231.                 GT_BeginRefresh(win);
  232.                 GT_EndRefresh(win, TRUE);
  233.                 break;
  234.             }
  235.             /* Reply when finished with message */
  236.             GT_ReplyIMsg(intuimsg);
  237.         }
  238.     }
  239.     /* Stop the patched function working */
  240.     ObtainSemaphore(&ready);
  241. }
  242.  
  243.